package nsip.scheduling;

import java.util.ArrayList;
import java.util.List;
import nsip.model.Item;

/**
 *
 * @author Markus Goetz
 */
public class Day {
    
    public static final int CYCLES_PER_DAY = 2000;

    private final int cyclesPerDay;
    private final int date;

    private int availableCycles;
    private int plannedCycles;

    private final List<Item> items = new ArrayList();
    private final List<Item> orders = new ArrayList();
    private final List<Item> rfqs = new ArrayList();
    
    public Day(int date, int cyclesPerDay) {
        this.date = date;
        this.availableCycles = cyclesPerDay;
        this.cyclesPerDay = cyclesPerDay;
        this.plannedCycles = 0;
    }

    public int getAvailableCycles() {
        return availableCycles;
    }

    /**
     * @return the dayNumber
     */
    public int getDate() {
        return date;
    }

    public double getUtilization() {
        return (plannedCycles + 0.0) / cyclesPerDay;
    }

    public void place(Item item) {
        availableCycles -= item.getProductionCyclesNeeded();
        plannedCycles += item.getProductionCyclesNeeded();

        items.add(item);

        if (item.isOrder() && item.isRfq()) {
            throw new IllegalStateException(
                item + " can not be Order and RFQ at the same time");
        }
        
        if (item.isOrder()) {
            orders.add(item);
        } else

        if (item.isRfq()) {
            rfqs.add(item);
        }
    }

    /**
     * @return the items
     */
    public List<Item> getItems() {
        return items;
    }
    
    /**
     * @return the orders
     */
    public List<Item> getOrders() {
        return orders;
    }

    /**
     * @return the rfqs
     */
    public List<Item> getRfqs() {
        return rfqs;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Day#").append(date);
        builder.append("(orders: ")
               .append(orders.size())
               .append(", rfqs: ")
               .append(rfqs.size())
               .append(", util: ")
               .append(getUtilization())
               .append(")");
               
        return builder.toString();
    }
}
